package com.cyzy.config;


import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;

@Component
public class MetaObjectHandlerConfig implements MetaObjectHandler {

    // 获取当前中国本地时间（UTC+8）
    private Timestamp getCurrentLocalTime() {
        // 指定时区为中国（UTC+8）
        ZonedDateTime zonedDateTime = ZonedDateTime.now(ZoneId.of("Asia/Shanghai"));
        return Timestamp.valueOf(zonedDateTime.toLocalDateTime());
    }


    @Override
    public void insertFill(MetaObject metaObject) {
        // 插入时自动填充createTime和updateTime
        this.strictInsertFill(metaObject, "createTime",
                Timestamp.class, Timestamp.valueOf(LocalDateTime.now()));
        this.strictInsertFill(metaObject, "updateTime",
                Timestamp.class, Timestamp.valueOf(LocalDateTime.now()));
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        // 插入时自动填充createTime和updateTime
        this.strictInsertFill(metaObject, "createTime",
                Timestamp.class, Timestamp.valueOf(LocalDateTime.now()));
        this.strictInsertFill(metaObject, "updateTime",
                Timestamp.class, Timestamp.valueOf(LocalDateTime.now()));
    }
}
